package com.yuan.blog.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.blog.dto.InvitationVo;
import com.yuan.blog.service.WebInvitationService;
import com.yuan.blog.utils.ApacheHttpUtils;
import com.yuan.blog.utils.HttpUtils;
import com.yuan.blog.utils.R;
import com.yuan.blog.dto.InvitataionStatusVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @program: blog-core
 * @description:
 * @author: yuan_boss
 * @create: 2023-01-05 15:29
 **/
@RestController
@RequestMapping("/InvitationController")
@Slf4j
public class SysInvitationController {

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private HttpUtils httpUtils;

    @Value("${blog.web.url}")
    private String baseUrl;


    @Value("${server.port}")
    private String port;

    @Autowired
    private WebInvitationService webInvitationService;

    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "system test:" + port;
    }

    @PostMapping("/page")
    @ResponseBody
    public R page(@RequestBody Page<InvitationVo> page){
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        HttpEntity httpEntity = new HttpEntity(page,headers);
//        ResponseEntity<Page> responseEntity = restTemplate.postForEntity(baseUrl + "InvitationController/page",
//                httpEntity, Page.class);
//        ResponseEntity responseEntity = httpUtils.post(baseUrl + "InvitationController/page", page);

        Page<InvitationVo> page1 = webInvitationService.page(page);
//        return R.ok(responseEntity.getBody());
        return R.ok(page1);
    }

    @PostMapping("/updateType")
    @ResponseBody
    public R updateType(@RequestBody InvitataionStatusVo vo){
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        HttpEntity httpEntity = new HttpEntity(vo,headers);
//        ResponseEntity responseEntity = restTemplate.postForEntity(baseUrl + "InvitationController/updateType",
//                httpEntity,Object.class);
//        ResponseEntity responseEntity = httpUtils.post(baseUrl + "InvitationController/updateType", vo);
//        return R.ok(responseEntity.getBody());

        Boolean updateType = webInvitationService.updateType(vo);
        return R.ok(updateType);
    }

    @GetMapping("/invitationInfo/{id}")
    public R invitationInfo(@PathVariable Integer id){
//        ResponseEntity responseEntity = httpUtils.get(baseUrl + "InvitationController/invitationInfo/" + id);
        InvitationVo invitationVo = webInvitationService.invitationInfo(id);
        return R.ok().put("data",invitationVo);
    }


    @PostMapping("/dataList")
    public R dataList(@RequestBody Page<InvitationVo> page) {
//        String s = JSON.toJSONString(page);
//        String json = ApacheHttpUtils.postJson(baseUrl + "InvitationController/dataList",s);
        Map map = webInvitationService.dataList(page);
        return R.ok().put("data",map);
    }

    public static void main(String[] args) {
        Page<InvitationVo> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        String s = JSON.toJSONString(page);
        String json = ApacheHttpUtils.postJson("http://localhost:8002/blog/InvitationController/dataList",s);
        System.out.println(JSON.parseArray(json, Map.class));


    }

}
